import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import viteConfitExtend from "./vite.config.extend";
// const url = 'http://gateway:10000'
// const fileUrl = "http://jvs-minio:9000"

const url = "https://knowledge.bctools.cn";
const fileUrl = "https://knowledge.bctools.cn";
const jfUrl = "http://121.36.255.127:7080";
const path = "jvs-knowledge-ui"; // fixme 请修改为自己的前端项目名称
// https://vitejs.dev/config/
export default defineConfig({
  define: {
    "process.env": process.env,
  },
  plugins: [
    vue(),
    viteConfitExtend({
      indexPath: `${path}/index.html`, // 相对于outDir的路径
    }),
  ],
  // 配置前端服务地址和端口
  server: {
    host: "0.0.0.0",
    port: 8081,
    maxMemory: "2048",
    // 是否开启 https
    // https: false,
    proxy: {
      "/mgr": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mgr/, "/mgr"),
        // pathRewrite: {
        //   '^/mgr': '/mgr'
        // }
      },
      "/auth": {
        target: jfUrl,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/auth/, "/auth"),
        // pathRewrite: {
        //   '^/auth': '/auth'
        // }
      },
      "/api": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "/api"),
        // pathRewrite: {
        //   '^/api': '/api'
        // }
      },
      "^/agreement": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/agreement/, "/agreement"),
        // pathRewrite: {
        //   "^/agreement": "/agreement"
        // }
      },
      "^/get": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/get/, "/get"),
        // pathRewrite: {
        //   "^/get": "/get"
        // }
      },
      "^/icon": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/icon/, "/icon"),
        // pathRewrite: {
        //   "^/icon": "/icon"
        // }
      },
      "^/im": {
        target: url,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/auth/, "/auth"),
        // pathRewrite: {
        //   "^/im": "/im"
        // }
      },
      "^/jvs-public": {
        target: fileUrl,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/jvs-public/, "/jvs-public"),
        // pathRewrite: {
        //   "^/jvs-public": "/jvs-public"
        // }
      },
      "^/jvs-document-file": {
        target: fileUrl,
        ws: true,
        secure: false,
        changeOrigin: true,
        rewrite: (path) =>
          path.replace(/^\/jvs-document-file/, "/jvs-document-file"),
        // pathRewrite: {
        //   "^/jvs-document-file": "/jvs-public"
        // }
      },
    },
  },
  resolve: {
    // ↓路径别名，主要是这部分
    alias: {
      "@": resolve(__dirname, "./src"),
    },
  },
  // 强制预构建插件包
  optimizeDeps: {
    include: [
      `monaco-editor/esm/vs/language/json/json.worker`,
      `monaco-editor/esm/vs/language/css/css.worker`,
      `monaco-editor/esm/vs/language/html/html.worker`,
      `monaco-editor/esm/vs/language/typescript/ts.worker`,
      `monaco-editor/esm/vs/editor/editor.worker`,
    ],
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @import 'src/styles/mixin.scss';
          @import 'src/styles/element-them.scss';
        `,
      },
      less: {
        math: "always", // 括号内才使用数学计算
        javascriptEnabled: true,
        globalVars: {
          // 全局变量
          mainColor: "red",
        },
      },
    },
  },
  build: {
    // assetsInlineLimit:0,
    chunkSizeWarningLimit: 1500,
    outDir: "docker/dist",
    emptyOutDir: true,
    assetsDir: path + "/static",
    rollupOptions: {
      // external: [
      //   "element-plus",
      // ],
      output: {
        // 分包
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
  },
});
